// mapCat 重新组装分类
export function mapCat (list) {
  try {
    // 去掉不需要的属性
    list = list.map(item => {
      return {
        id: item.id,
        level: item.level,
        name: item.name,
        parentID: item.parentID
      };
    });
    let catData = [];
    let catL1 = list.filter(item => item.level === 1);
    let catL2 = list.filter(item => item.level === 2);

    catL1.forEach(L1 => {
      let level2 = catL2.filter(L2 => L2.parentID === L1.id);
      L1.children = level2;
      catData.push(L1);
    })

    console.log('map分类', catData);
    return catData;
  } catch (e) {
    console.error(e);
  }
}
// mapGood 重新组装商品
export function mapGoods (goods) {
  try {
    let arr = [];
    goods.forEach(skuName => {
      skuName.skus.forEach(sku => {
        arr.push({
          name: getGoodName(skuName, sku),
          id: sku.id,
          img: skuName.img + '?imageView2/1/w/136/h/136',
          price: (sku.price / 100).toFixed(2),
          count: 0
        });
      });
    });
    console.log('mapGoods', arr);
    return arr;
  } catch (e) {
    return [];
  }
}
// 组装名称
export function getGoodName (skuName, sku) {
  let goodName = '';
  if (skuName.prefix) goodName += `[${skuName.prefix}]`;
  goodName += skuName.name;
  if (skuName.unit === '份') {
    goodName += '约';
  }
  goodName += `${sku.specQuality}${sku.specUnit}/${skuName.unit}`;
  if (sku.comment) goodName += `(${sku.comment})`;

  return goodName;
}
